package com.hdit.guimei.web;

import com.hdit.guimei.domain.Seckill_time;
import com.hdit.guimei.mapper.SeckillMapper;
import com.hdit.guimei.service.impl.SeckillServiceImpl;
import com.hdit.guimei.service.impl.Seckill_timeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 张宝玉
 * @date 2021/1/20 16:50
 */
@Controller
public class Seckill_timeController extends HttpServlet {

    @Autowired
    public SeckillMapper seckillMapper;
    @Autowired
    public Seckill_timeServiceImpl seckillTimeService;

    @RequestMapping("/findseckilltimebyid")
    public String findseckilltimebyid(int seckill_id, Model m){
        Seckill_time seckill_time = seckillTimeService.findseckilltime(seckill_id);
        m.addAttribute("start_time",seckill_time.getStart_time());
        m.addAttribute("end_time",seckill_time.getEnd_time());
        return "";
    }
    @RequestMapping("deleseckilltimebyid")
    public String deleseckilltimebyid(int seckill_id){
        int i = seckillTimeService.deleseckilltime(seckill_id);
        if (i==1){
            int i1 = seckillMapper.delegoodinseckillbyseckill_id(seckill_id);
            if (i1==1){
                return "";
            }else {
                return "";
            }
        }else{
            return "";
        }
    }
    @RequestMapping("/addseckilltime")
    public  String addseckilltime(@RequestBody Seckill_time seckillTime){
        int i = seckillTimeService.addseckilltime(seckillTime);
        if (i==1){
            return "";
        }else{
            return "";
        }
    }
    @RequestMapping("/updateseckilltime")
    public String updateseckilltime(String start_time,String end_time){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        try {
            Date time_start = sdf.parse(start_time);
            Date time_end= sdf.parse(end_time);
            int i = seckillTimeService.updateseckilltime(time_start, time_end);
            if (i==1){
                return "";
            }else{
                return "";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
}
